﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TableParsers;
using Speakup;
using System.ComponentModel;
using DBL = DatabaseLayer;

namespace Frontend
{
    /// <summary>
    /// Interaction logic for WeeksControl.xaml
    /// </summary>
    public partial class WeeksControl : UserControl
    {
        public static readonly DependencyProperty CurrentWeekProperty;
        public static readonly DependencyProperty CurrentLevelProperty;

        PersonalStats m_stats = null;
        public PersonalStats Stats
        {
            get
            {
                return m_stats;
            }
            set
            {
                m_stats = value;
                DigestThemeGrid.Stats = value;
                GrammarThemeGrid.Stats = value;
                ConversationThemeGrid.Stats = value;
                WritingThemeGrid.Stats = value;
                ReadingThemeGrid.Stats = value;
            }
        }

        static WeeksControl()
        {
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.DefaultValue = 1;
            CurrentWeekProperty = DependencyProperty.Register("CurrentWeek", typeof(int), 
                                                       typeof(WeeksControl), metadata);

            metadata = new FrameworkPropertyMetadata();
            metadata.DefaultValue = 1;
            CurrentLevelProperty = DependencyProperty.Register("CurrentLevel", typeof(int), 
                                                        typeof(WeeksControl), metadata);
        }

        public int CurrentLevel
        {
            get
            {
                return (int)GetValue(CurrentLevelProperty);
            }
            set
            {
                SetValue(CurrentLevelProperty, value);
            }
        }
        public int CurrentWeek
        {
            get
            {
                return (int)GetValue(CurrentWeekProperty);
            }
            set
            {
                SetValue(CurrentWeekProperty, value);
            }
        }

        private DBL.SpeakupEntities m_dbCtx = null;
        public DBL.SpeakupEntities DbCtx
        {
            get
            {
                if (m_dbCtx == null)
                {
                    m_dbCtx = DBL.DatabaseContextFactory.Create();
                }
                return m_dbCtx;
            }
            set
            {
                m_dbCtx = value;
            }
        }

        bool m_isWeekEditingEnabled = false;
        bool IsWeekEditingEnabled
        {
            get
            {
                return m_isWeekEditingEnabled;
            }
            set
            {
                m_isWeekEditingEnabled = value;
                    
                Visibility viewVisibility = Visibility.Visible;
                Visibility editVisibility = Visibility.Collapsed;

                if (m_isWeekEditingEnabled)
                {
                    viewVisibility = Visibility.Collapsed;
                    editVisibility = Visibility.Visible;
                }

                CurrentWeekLabel.Visibility = viewVisibility;
                CurrentWeekValue.Visibility = viewVisibility;
                CurrentWeekChangeLink.Visibility = viewVisibility;
                CurrentWeekComboBox.Visibility = editVisibility;
                CurrentWeekApplyLink.Visibility = editVisibility;
            }
        }

        public WeeksControl()
        {
            InitializeComponent();

            WeekThemeGrid[] grids = { DigestThemeGrid, 
                                        GrammarThemeGrid,
                                        ConversationThemeGrid, 
                                        WritingThemeGrid, 
                                        ReadingThemeGrid};

            foreach (WeekThemeGrid grid in grids)
            {
                grid.DbCtx = DbCtx;
                grid.HighlitedWeekThemeChanged += WeekThemeGrid_HighlitedWeekThemeChanged;
            }

            //observe changes to CurrentWeek and CurrentLevel
            {
                DependencyPropertyDescriptor currentWeekDescriptor =
                    DependencyPropertyDescriptor.FromProperty(CurrentWeekProperty, typeof(WeeksControl));

                currentWeekDescriptor.AddValueChanged(this, CurrentWeekChanged);

                DependencyPropertyDescriptor currentLevelDescriptor =
                    DependencyPropertyDescriptor.FromProperty(CurrentLevelProperty, typeof(WeeksControl));

                currentLevelDescriptor.AddValueChanged(this, CurrentLevelChanged);
            }

            FillWeeksComboBox();
            
            //IsWeekEditingEnabled = false;

            CurrentWeekValue.Text = CurrentWeek.ToString();
            CurrentLevelValue.Text = CurrentLevel.ToString();

            RefreshGrids();
        }

        void FillWeeksComboBox()
        {
            CurrentWeekComboBox.Items.Clear();
            foreach (int week in WeekTheme.SupportedWeeks(DbCtx))
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Inlines.Add(new Run("Week "));
                textBlock.Inlines.Add(new Bold( new Run(week.ToString()) ));

                ComboBoxItem item = new ComboBoxItem();
                item.Content = textBlock;
                item.Tag = week;

                if (week == CurrentWeek)
                {
                    item.IsSelected = true;
                }
                
                CurrentWeekComboBox.Items.Add(item);
            }

            
        }

        void CurrentWeekChanged(object sender, EventArgs e)
        {
            CurrentWeekValue.Text = CurrentWeek.ToString();
            RefreshGrids();
        }

        void CurrentLevelChanged(object sender, EventArgs e)
        {
            CurrentLevelValue.Text = CurrentLevel.ToString();
            RefreshGrids();
        }

        void WeekThemeGrid_HighlitedWeekThemeChanged(object sender, EventArgs e)
        {
            WeekThemeGrid grid = (WeekThemeGrid)sender;
            WeekTheme weekTheme = grid.HighlightedWeekTheme;

            if (weekTheme == null)
            {
                HighlightedClassType.Text = "";
                HighlightedWeek.Text = "";
                HighlightedLevel.Text = "";
                HighlightedTheme.Text = "";
            }
            else
            {
                HighlightedClassType.Text = weekTheme.ClassType.ToString();
                HighlightedLevel.Text = String.Format("Level {0}",weekTheme.Level);
                HighlightedWeek.Text = String.Format("Week {0:00}",weekTheme.Week);
                HighlightedTheme.Text = weekTheme.Theme;
            }
        }

        public void RefreshGrids()
        {
            DigestThemeGrid.SetupGrid(HeaderType.Week, HeaderType.ClassType, 
                                                CurrentWeek, CurrentLevel, null);

            GrammarThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, ClassType.Grammar);

            ConversationThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, ClassType.Conversation);

            WritingThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, ClassType.Writing);

            ReadingThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, ClassType.Reading);            
        }

        private void ChangeCurrentWeek_Click(object sender, RoutedEventArgs e)
        {
            IsWeekEditingEnabled = true;
        }

        private void ApplyCurrentWeek_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentWeekComboBox.SelectedItem != null)
            {
                ComboBoxItem item = (ComboBoxItem)CurrentWeekComboBox.SelectedItem;
                CurrentWeek = (int)item.Tag;
            }
            IsWeekEditingEnabled = false;
        }
    }
}
